home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 19 / Amiga Plus Leser CD 19.iso / Tools / MorphOS / cvs-1.11.2 / source / amiga / _assert.c next >
Encoding:
C/C++ Source or Header  |  2002-11-18  |  7.8 KB  |  386 lines

  1. /*
  2.  * $Id$
  3.  *
  4.  * :ts=4
  5.  *
  6.  * AmigaOS wrapper routines for GNU CVS, using the RoadShow TCP/IP API
  7.  *
  8.  * Written and adapted by Olaf `Olsen' Barthel <olsen@sourcery.han.de>
  9.  *                        Jens Langner <Jens.Langner@light-speed.de>
  10.  *
  11.  * This program is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; if not, write to the Free Software
  23.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  */
  25.  
  26. /****************************************************************************/
  27.  
  28. #include "SDI_compiler.h"
  29.  
  30. #include <proto/exec.h>
  31. #include <proto/dos.h>
  32.  
  33. #include <string.h>
  34.  
  35. extern void kprintf(const char *,...);
  36. extern void STDARGS kputc(char c);
  37.  
  38. /****************************************************************************/
  39.  
  40. #include <stdarg.h>
  41.  
  42. /****************************************************************************/
  43.  
  44. #define DEBUGLEVEL_OnlyAsserts   0
  45. #define DEBUGLEVEL_Reports 1
  46. #define DEBUGLEVEL_CallTracing   2
  47.  
  48. /****************************************************************************/
  49.  
  50. static int indent_level = 0;
  51. static int debug_level = DEBUGLEVEL_CallTracing;
  52.  
  53. static char program_name[40];
  54. static int program_name_len = 0;
  55.  
  56. /****************************************************************************/
  57.  
  58. void
  59. _SETPROGRAMNAME(char *name)
  60. {
  61.    if(name != NULL && name[0] != '\0')
  62.    {
  63.       program_name_len = strlen(name);
  64.       if(program_name_len >= sizeof(program_name))
  65.          program_name_len = sizeof(program_name)-1;
  66.  
  67.       strncpy(program_name,name,program_name_len);
  68.       program_name[program_name_len] = '\0';
  69.    }
  70.    else
  71.    {
  72.       program_name_len = 0;
  73.    }
  74. }
  75.  
  76. /****************************************************************************/
  77.  
  78. int
  79. _SETDEBUGLEVEL(int level)
  80. {
  81.    int old_level = debug_level;
  82.  
  83.    debug_level = level;
  84.  
  85.    return(old_level);
  86. }
  87.  
  88. /****************************************************************************/
  89.  
  90. int
  91. _GETDEBUGLEVEL(void)
  92. {
  93.    return(debug_level);
  94. }
  95.  
  96. /****************************************************************************/
  97.  
  98. static int previous_debug_level = -1;
  99.  
  100. void
  101. _PUSHDEBUGLEVEL(int level)
  102. {
  103.    previous_debug_level = _SETDEBUGLEVEL(level);
  104. }
  105.  
  106. void
  107. _POPDEBUGLEVEL(void)
  108. {
  109.    if(previous_debug_level != -1)
  110.    {
  111.       _SETDEBUGLEVEL(previous_debug_level);
  112.  
  113.       previous_debug_level = -1;
  114.    }
  115. }
  116.  
  117. /****************************************************************************/
  118.  
  119. void
  120. _INDENT(void)
  121. {
  122.    if(program_name_len > 0)
  123.       kprintf("(%s) ",program_name);
  124.  
  125.    if(debug_level >= DEBUGLEVEL_CallTracing)
  126.    {
  127.       int i;
  128.  
  129.       for(i = 0 ; i < indent_level ; i++)
  130.          kprintf("   ");
  131.    }
  132. }
  133.  
  134. /****************************************************************************/
  135.  
  136. void
  137. _SHOWVALUE(
  138.    unsigned long value,
  139.    int size,
  140.    const char *name,
  141.    const char *file,
  142.    int line)
  143. {
  144.    if(debug_level >= DEBUGLEVEL_Reports)
  145.    {
  146.       char *fmt;
  147.  
  148.       switch(size)
  149.       {
  150.          case 1:
  151.  
  152.             fmt = "%s:%ld:%s = %ld, 0x%02lx";
  153.             break;
  154.  
  155.          case 2:
  156.  
  157.             fmt = "%s:%ld:%s = %ld, 0x%04lx";
  158.             break;
  159.  
  160.          default:
  161.  
  162.             fmt = "%s:%ld:%s = %ld, 0x%08lx";
  163.             break;
  164.       }
  165.  
  166.       _INDENT();
  167.  
  168.       kprintf(fmt,file,line,name,value,value);
  169.  
  170.       if(size == 1 && value < 256)
  171.       {
  172.          if(value < ' ' || (value >= 127 && value < 160))
  173.             kprintf(", '\\x%02lx'",value);
  174.          else
  175.             kprintf(", '%lc'",value);
  176.       }
  177.  
  178.       kprintf("\n");
  179.    }
  180. }
  181.  
  182. /****************************************************************************/
  183.  
  184. void
  185. _SHOWSTRING(
  186.    const char *string,
  187.    const char *name,
  188.    const char *file,
  189.    int line)
  190. {
  191.    if(debug_level >= DEBUGLEVEL_Reports)
  192.    {
  193.       _INDENT();
  194.       kprintf("%s:%ld:%s = 0x%08lx \"%s\"\n",file,line,name,string,string);
  195.    }
  196. }
  197.  
  198. /****************************************************************************/
  199.  
  200. void
  201. _SHOWMSG(
  202.    const char *string,
  203.    const char *file,
  204.    int line)
  205. {
  206.    if(debug_level >= DEBUGLEVEL_Reports)
  207.    {
  208.       _INDENT();
  209.       kprintf("%s:%ld:%s\n",file,line,string);
  210.    }
  211. }
  212.  
  213. /****************************************************************************/
  214.  
  215. void
  216. _DPRINTF_HEADER(
  217.    const char *file,
  218.    int line)
  219. {
  220.    if(debug_level >= DEBUGLEVEL_Reports)
  221.    {
  222.       _INDENT();
  223.       kprintf("%s:%ld:",file,line);
  224.    }
  225. }
  226.  
  227. #if defined(__MORPHOS__)
  228. static void putch()
  229. {
  230.   char c = REG_D0;
  231. #else
  232. static ASM(void) putch(REG(d0, char c))
  233. {
  234. #endif
  235.    if(c != '\0')
  236.       kputc(c);
  237. }
  238.  
  239. void
  240. _DPRINTF(const char *fmt,...)
  241. {
  242.    if(debug_level >= DEBUGLEVEL_Reports)
  243.    {
  244.       va_list args;
  245.  
  246.       va_start(args,fmt);
  247.       RawDoFmt((char *)fmt,args,(VOID (*)())putch,NULL);
  248.       va_end(args);
  249.  
  250.       kprintf("\n");
  251.    }
  252. }
  253.  
  254. void
  255. _DLOG(const char *fmt,...)
  256. {
  257.    if(debug_level >= DEBUGLEVEL_Reports)
  258.    {
  259.       va_list args;
  260.  
  261.       va_start(args,fmt);
  262.       RawDoFmt((char *)fmt,args,(VOID (*)())putch,NULL);
  263.       va_end(args);
  264.    }
  265. }
  266.  
  267. /****************************************************************************/
  268.  
  269. void
  270. _ENTER(
  271.    const char *file,
  272.    int line,
  273.    const char *function)
  274. {
  275.    if(debug_level >= DEBUGLEVEL_CallTracing)
  276.    {
  277.       _INDENT();
  278.       kprintf("%s:%ld:Entering %s\n",file,line,function);
  279.    }
  280.  
  281.    indent_level++;
  282. }
  283.  
  284. void
  285. _LEAVE(
  286.    const char *file,
  287.    int line,
  288.    const char *function)
  289. {
  290.    indent_level--;
  291.  
  292.    if(debug_level >= DEBUGLEVEL_CallTracing)
  293.    {
  294.       _INDENT();
  295.       kprintf("%s:%ld: Leaving %s\n",file,line,function);
  296.    }
  297. }
  298.  
  299. void
  300. _RETURN(
  301.    const char *file,
  302.    int line,
  303.    const char *function,
  304.    unsigned long result)
  305. {
  306.    indent_level--;
  307.  
  308.    if(debug_level >= DEBUGLEVEL_CallTracing)
  309.    {
  310.       _INDENT();
  311.       kprintf("%s:%ld: Leaving %s (result 0x%08lx, %ld)\n",file,line,function,result,result);
  312.    }
  313. }
  314.  
  315. /****************************************************************************/
  316.  
  317. void
  318. _ASSERT(
  319.    int x,
  320.    const char *xs,
  321.    const char *file,
  322.    int line,
  323.    const char *function)
  324. {
  325.    #ifdef CONFIRM
  326.    {
  327.       STATIC BOOL ScrollMode  = FALSE;
  328.       STATIC BOOL BatchMode   = FALSE;
  329.  
  330.       if(BatchMode == FALSE)
  331.       {
  332.          if(x == 0)
  333.          {
  334.             kprintf("%s:%ld:Expression `%s' failed assertion in %s().\n",
  335.                     file,
  336.                     line,
  337.                     xs,
  338.                     function);
  339.  
  340.             if(ScrollMode == FALSE)
  341.             {
  342.                ULONG Signals;
  343.  
  344.                SetSignal(0,SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D | SIGBREAKF_CTRL_E);
  345.  
  346.                kprintf(" ^C to continue, ^D to enter scroll mode, ^E to enter batch mode\r");
  347.  
  348.                Signals = Wait(SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D | SIGBREAKF_CTRL_E);
  349.  
  350.                if(Signals & SIGBREAKF_CTRL_D)
  351.                {
  352.                   ScrollMode = TRUE;
  353.  
  354.                   kprintf("Ok, entering scroll mode\033[K\n");
  355.                }
  356.                else if (Signals & SIGBREAKF_CTRL_E)
  357.                {
  358.                   BatchMode = TRUE;
  359.  
  360.                   kprintf("Ok, entering batch mode\033[K\n");
  361.                }
  362.                else
  363.                {
  364.                   /* Continue */
  365.  
  366.                   kprintf("\033[K\r");
  367.                }
  368.             }
  369.          }
  370.       }
  371.    }
  372.    #else
  373.    {
  374.       if(x == 0)
  375.       {
  376.          _INDENT();
  377.          kprintf("%s:%ld:Expression `%s' failed assertion in %s().\n",
  378.                  file,
  379.                  line,
  380.                  xs,
  381.                  function);
  382.       }
  383.    }
  384.    #endif   /* CONFIRM */
  385. }
  386.